/*
 * Copyright (C) 2017 yixun All Rights Reserved.
 * 
 * CustTagServiceImpl.java
 */
package com.emm.yixun.basic.service.impl;

import java.util.Calendar;
import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.emm.yixun.common.page.Page;
import com.emm.yixun.common.app.servletentity.CustTaglistRes;
import com.emm.yixun.common.model.CustTag;
import com.emm.yixun.common.model.CustTagInfo;
import com.emm.yixun.common.model.dto.CustTagDto;
import com.emm.yixun.basic.service.CustTagService;
import com.emm.yixun.basic.mapper.CustTagMapper;


/**
 * [文件名称]<br>
 * CustTagServiceImpl.java<br>
 * <br>
 * [文件描述]<br>
 * 客户标签信息表 service interface impl<br>
 * <br>
 * [修改记录]<br>
 * 2017-11-01 15:00:17 创建 陶俊代码生成器<br>
 * 
 * @author 陶俊代码生成器
 * @version 1.00
 */
@Service("custTagService")
public class CustTagServiceImpl implements CustTagService {
	
	@Autowired
	private CustTagMapper mapper;
	
	/**
	 * @param entity 搜索条件
	 * @param begin 分页开始行数[不包含]
	 * @param end 分页结束行数[包含]
	 * @return 分页记录
	 */
	@Transactional(readOnly = true)
	public Page<CustTag> getPageModel(CustTagDto entity,int begin,int end){
		Integer total = mapper.count(entity);
		List<CustTag> rows = mapper.list(entity,begin,end);
	    Page<CustTag> msr = new Page<CustTag>(total, rows);
	    msr.setPageSize(end);
		return msr;
	}

	/**
	 * 查询列表
	 * @param entity 搜索条件
	 * @return 分页记录
	 */
	public List<CustTag> selectListByDto(CustTagDto entity){
		return mapper.selectListByDto(entity);
	}
	
	/**
	 * 查询单个
	 * @param entity 搜索条件
	 * @return 分页记录
	 */
	public CustTag selectByDto(CustTagDto entity){
		return mapper.selectByDto(entity);
	}
	
	/**
	 * @param entity 搜索条件
	 * @return 分页记录
	 */
	public CustTag selectById(Long id){
		if(id!=null){
			return mapper.selectById(id);
		}
		return null;
	}
	/**
	 * @param entity 新增记录的详细信息
	 * @return 新增行数
	 */
	public CustTagDto create(CustTagDto entity) {
		entity.setCreateTime(Calendar.getInstance().getTime());
		entity.setUpdateTime(Calendar.getInstance().getTime());
		mapper.add(entity);
		return entity;
	}

	/**
	 * @param entity 更新记录的详细信息
	 * @return 更新行数
	 */
	public int update(CustTagDto entity) {
		entity.setUpdateTime(Calendar.getInstance().getTime());
		return mapper.edit(entity);
	}
    
	/**
	 * @param entity 待删除的记录信息
	 * @return 删除行数
	 */
	public int destroy(CustTagDto entity) {
		return mapper.del(entity);
	}
	
	/**
	 * @param entity 新增或更新
	 * @return 新增行数
	 */
	public int saveOrUpdate(CustTagDto entity) {
		if(entity!=null&&entity.getId()!=null){
			return update(entity);
		}else{
			create(entity);
			return 1;
		}
	}
	
	/**
     * @param entity 搜索条件
     * @param begin 分页开始行数[不包含]
     * @param end 分页结束行数[包含]
     * @return 分页记录
     */
    @Transactional(readOnly = true)
    public Page<CustTagInfo> getCusTagPage(CustTagDto entity,int begin,int end){
        Integer total = mapper.cusTagCount(entity);
        List<CustTagInfo> rows = mapper.cusTaglist(entity,begin,end);
        Page<CustTagInfo> msr = new Page<CustTagInfo>(total, rows);
        msr.setPageSize(end);
        return msr;
    }
    
    /**
     * @param entity 搜索条件
     * @param begin 分页开始行数[不包含]
     * @param end 分页结束行数[包含]
     * @return 分页记录
     */
    @Transactional(readOnly = true)
    public Page<CustTagInfo> getCusTagInfoPage(CustTagDto entity,int begin,int end){
        Integer total = mapper.cusTagInfoCount(entity);
        List<CustTagInfo> rows = mapper.cusTagInfolist(entity,begin,end);
        Page<CustTagInfo> msr = new Page<CustTagInfo>(total, rows);
        msr.setPageSize(end);
        return msr;
    }
    
    /**
     * @param entity 搜索条件
     * @param begin 分页开始行数[不包含]
     * @param end 分页结束行数[包含]
     * @return 分页记录
     */
    @Transactional(readOnly = true)
    public Page<CustTaglistRes> getCustomerTagPage(CustTagDto entity,int begin,int end){
        Integer total = mapper.customerTagCount(entity);
        List<CustTaglistRes> rows = mapper.customerTaglist(entity,begin,end);
        Page<CustTaglistRes> msr = new Page<CustTaglistRes>(total, rows);
        msr.setPageSize(end);
        return msr;
    }
    
    /**
     * @param entity 搜索条件
     * @param begin 分页开始行数[不包含]
     * @param end 分页结束行数[包含]
     * @return 分页记录
     */
    @Transactional(readOnly = true)
    public Page<CustTaglistRes> customerQuestionnairePage(CustTagDto entity, String projectCode, int begin,int end){
        Integer total = mapper.customerQuestionnaireCount(entity, projectCode);
        List<CustTaglistRes> rows = mapper.customerQuestionnaireList(entity, projectCode, begin,end);
        Page<CustTaglistRes> msr = new Page<CustTaglistRes>(total, rows);
        msr.setPageSize(end);
        return msr;
    }
    
    /**
     * 获取问题答案
     * @param projectId
     * @param question
     * @param customerName
     * @param customerPhone
     * @return
     */
    public List<CustTaglistRes> getCustomerAnswerList(Long projectId, Long questionnaireId){
    	return mapper.getCustomerAnswerList(projectId, questionnaireId);
    }
	
    /**
     * 通过问题ID和客户ID获取标签答案ID
     * @param questionId 问题ID
     * @param customerId 客户ID
     * @return 标签答案ID
     */
	public Long getCusTagIdByQuestionId(String questionId, Long customerId) {
		return mapper.getCusTagIdByQuestionId(questionId, customerId);
	}
}
